package com.teach.sys.service.impl;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.annotation.PostConstruct;
import javax.imageio.ImageIO;

import org.springframework.stereotype.Service;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

import com.teach.CodeException;
import com.teach.config.ConfigProperties;
import com.teach.sys.service.FileService;

@Service("aliyunOss")
@Slf4j
public class FileServiceByAliyunImpl implements FileService {

	private ConfigProperties configProperties;

	public FileServiceByAliyunImpl(ConfigProperties configProperties) {
		this.configProperties = configProperties;
	}

	@Override
	public String save(InputStream in, String name) {
		try {
			if (in == null || in.available() == 0) {
				throw new CodeException(404);
			}

			OSS ossClient = null;
			try {
				// 创建OSSClient实例。
				ossClient = new OSSClientBuilder().build(configProperties.aliyunOss.endpoint,
						configProperties.aliyunOss.accessKeyId, configProperties.aliyunOss.accessKeySecret);
				// 上传文件。
				ossClient.putObject(configProperties.aliyunOss.bucket, name, in);
				return configProperties.aliyunOss.pre + name;
			} catch (Exception e) {
				throw new CodeException(400, e);
			} finally {
				if (ossClient != null) {
					ossClient.shutdown();
				}
			}
		} catch (Exception e) {
			if (e instanceof CodeException) {
				throw (CodeException) e;
			}
			throw new CodeException(401, e);
		}
	}

	@Override
	public void delete(String filename) {
		if (StrUtil.isEmpty(filename)) {
			throw new CodeException(403);
		}
		try {
			if (filename.startsWith(configProperties.aliyunOss.pre)) {
				filename = filename.substring(configProperties.aliyunOss.pre.length());
			}
			if (filename.indexOf("?") > -1) {
				filename = filename.substring(0, filename.indexOf("?"));
			}
			OSS ossClient = null;
			try {
				// 创建OSSClient实例。
				ossClient = new OSSClientBuilder().build(configProperties.aliyunOss.endpoint,
						configProperties.aliyunOss.accessKeyId,
						configProperties.aliyunOss.accessKeySecret);
				// 上传文件。
				ossClient.deleteObject(configProperties.aliyunOss.bucket, filename);

			} catch (Exception e) {
				throw new CodeException(405, e);
			} finally {
				if (ossClient != null) {
					ossClient.shutdown();
				}
			}

		} catch (Exception e) {
			if (e instanceof CodeException) {
				throw (CodeException) e;
			}
			throw new CodeException(406, e);
		}
	}

	private BufferedImage image = null;

	@Override
	public BufferedImage getWatermark() {
		return image;
	}

	/**
	 * bean初始化时执行一次
	 */
	@PostConstruct
	public void init() {
		String watermark = configProperties.watermark;
		if (StrUtil.isBlank(watermark)) {
			log.warn("未配置图片的水印");
			return;
		}
		if (watermark.toLowerCase().startsWith("classpath:")) {//加载classpath水印图片
			try {
				image = ImageIO.read(this.getClass().getResourceAsStream(watermark.substring(10)));
				log.info("加载classpath水印图片成功"+watermark);
			} catch (IOException e) {
				log.warn("加载classpath水印图片失败:" + watermark);
			}
		} else if (watermark.contains("://")) {// 加载网络水印图片
			log.debug("开始下载网络水印图片:" + watermark);
			HttpResponse response = HttpUtil.createGet(watermark).execute();
			if (response.isOk()) {
				try {
					image = ImageIO.read(response.bodyStream());
					log.info("加载网络水印图片成功"+watermark);
				} catch (IOException e) {
					log.warn("获取网络水印图片失败:" + watermark);
				}
			} else {
				log.warn("获取网络水印图片失败:" + watermark);
			}
		} else {//加载本地文件水印图片
			try {
				image = ImageIO.read(new File(watermark));
				log.info("加载本地文件水印图片成功"+watermark);
			} catch (IOException e) {
				log.warn("加载本地文件水印图片失败:" + watermark);
			}
		}
	}

}